﻿using Microsoft.AspNetCore.Mvc;
using Qy.Common;
using Qy.Models;
using Qy.Service;
using Qy.Web.Authorize;
using System;
using System.Threading.Tasks;

namespace Qy.Web.Api
{
    [Route("Api/[controller]/[action]")]
    [Verify]
    public class UserDeptController : Base
    {
        private readonly LoginUtil _LoginUtil;
        private readonly UserDeptService _UserDeptService;
        private readonly ContentCateService _contentCateService;
        public UserDeptController(
            LoginUtil LoginUtil,
            UserDeptService adminUserDeptService,
            ContentCateService contentCateService
            )
        {
            _LoginUtil = LoginUtil;
            _UserDeptService = adminUserDeptService;
            _contentCateService = contentCateService;
        }


        [HttpGet]
        [ActionPermissionFilter(Permission = PermissionKeys.UserDept_Index)]
        public async Task<IActionResult> IndexAsync()
        {
            var pagedList = await _UserDeptService.GetUserDepts();
            return Json(new { status = 1, data = pagedList });
        }

        [HttpGet]
        [ActionPermissionFilter(Permission = PermissionKeys.UserDept_AddOrEditView)]
        public async Task<IActionResult> AddOrEditAsync(int id)
        {
            var deptList = await _UserDeptService.GetUserDeptListAsync();
            if (id < 1)
            {
                return Json(new { status = 1, deptList });
            }
            else
            {
                var data = await _UserDeptService.GetInfo(id);
                return Json(new { status = 1, data, deptList });
            }

        }


        [HttpPost, ActionName("Create")]
        [XssFieldFilter]//防Xss
        [ActionPermissionFilter(Permission = PermissionKeys.UserDept_AddOrEdit)]
        [Log(Name = "新增部门")]
        public async Task<IActionResult> CreatePostAsync([FromBody] EditUserDept cateModel)
        {
            if (ModelState.IsValid)
            {
                cateModel.GreatTime = DateTime.Now;
                object insertPri = await _UserDeptService.InsertAsync(cateModel);
                return Json(new { status = 1, msg = "新增部门 ID:" + insertPri.ToString() });
            }
            else
            {
                var error = ModelState.GetErrorMessages();
                return Json(new { status = 0, msg = error });
            }
        }


        [HttpPut, ActionName("Edit")]
        [XssFieldFilter]//防Xss
        [ActionPermissionFilter(Permission = PermissionKeys.UserDept_AddOrEdit)]
        [Log(Name = "修改部门")]
        public async Task<IActionResult> EditAsync([FromBody] EditUserDept editModel, int dyEdit = 0)
        {

            if (ModelState.IsValid)
            {
                UserDept adminUserDept = await _UserDeptService.GetInfo(editModel.Id);
                if (dyEdit == 1)
                {
                    //此功能前端暂时没有实现
                    adminUserDept.OrderNum = editModel.OrderNum;

                    if (0 < await _UserDeptService.UpdateAsync(adminUserDept))
                        return Json(new { status = 1, msg = "修改部门排序:" + adminUserDept.DeptName + " Id： " + editModel.Id });
                    else
                        return Json(new { status = 0, msg = "修改部门失败:" + adminUserDept.DeptName + " Id： " + editModel.Id });

                }

                adminUserDept.Pid = editModel.Pid;
                adminUserDept.DeptName = editModel.DeptName;
                adminUserDept.OrderNum = editModel.OrderNum;
                adminUserDept.Remark = editModel.Remark;
                if (0 < await _UserDeptService.UpdateAsync(adminUserDept))
                    return Json(new { status = 1, msg = "修改部门:" + adminUserDept.DeptName + " Id： " + editModel.Id });
                else
                    return Json(new { status = 0, msg = "修改部门:" + adminUserDept.DeptName + " Id： " + editModel.Id });
            }
            else
            {
                var error = ModelState.GetErrorMessages();
                return Json(new { status = 0, msg = error });
            }
        }

        [HttpDelete, ActionName("Delete")]
        [ActionPermissionFilter(Permission = PermissionKeys.UserDept_Delete)]
        [Log(Name = "删除部门")]
        public async Task<IActionResult> DeleteAsync(int id)
        {
            if (await _UserDeptService.DeleteByPriKeyAsync(id) > 0)
                return Json(new { status = 1, msg = "删除部门,Id： " + id });

            return Json(new { status = 0, msg = "删除失败！当前部门下有用户！" });
        }

        /// <summary>
        /// 修改网站栏目
        /// </summary>
        [HttpGet]
        [ActionPermissionFilter(Permission = PermissionKeys.UserDept_AddOrEditView)]
        public async Task<IActionResult> EditContentCate(int id)
        {
            if (id < 1)
                return Json(new { status = 0, msg = "参数错误！" });
            var data = await _UserDeptService.GetInfo(id);
            var allTree = await _contentCateService.GetCateTree(await _LoginUtil.UserInfoAsync());
            UserDeptCateRole adminUserDept = new()
            {
                Id = data.Id,
                ContentCateRole = data.ContentCateRole,
                DeptName = data.DeptName,
            };

            return Json(new { status = 1, data = adminUserDept, allTree });
        }

        [HttpPut]
        [XssFieldFilter]//防Xss
        [ActionPermissionFilter(Permission = PermissionKeys.UserDept_AddOrEdit)]
        [Log(Name = "网站栏目资源")]
        public async Task<IActionResult> ContentCateEdit([FromBody] UserDeptCateRole adminUserGroup)
        {
            if (0 < await _UserDeptService.UpdateUserDeptCateRoleAsync(adminUserGroup))
                return Json(new { status = 1, msg = "修改部门网站栏目权限 ID：" + adminUserGroup.Id });

            return Json(new { status = 0, msg = "修改部门网站栏目权限 ID：" + adminUserGroup.Id });
        }
    }
}
